package com.meiyu.live.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.meiyu.live.R;
import com.meiyu.live.popj.Anchor;


/**
 * 邀约主播对话框
 */
public class AppointDialog extends Dialog implements View.OnClickListener {

    private Context context;

    private TextView tv_count, tv_coins, tv_bought_count;
    private View btn_minus, btn_add, btn_ok, btn_cancel;

    private int count = 1;
    private int mPrice = 10;
    private Anchor anchorBean;
    private AppointDialogListener appointDialogListener;

    public interface AppointDialogListener {
        void onBuy(long verify_id, int cost);

        void onCancel();
    }

    public AppointDialog(Context context) {
        super(context, R.style.dialog_custom);
        this.context = context;
        init(context);
    }

    public AppointDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_appoint);
        setCanceledOnTouchOutside(false);

        tv_count = (TextView) findViewById(R.id.tv_count);
        tv_count.setOnClickListener(this);
        tv_bought_count = (TextView) findViewById(R.id.tv_bought_count);
        tv_coins = (TextView) findViewById(R.id.tv_coins);
        tv_coins.setOnClickListener(this);
        btn_minus = findViewById(R.id.btn_minus);
        btn_minus.setOnClickListener(this);
        btn_add = findViewById(R.id.btn_add);
        btn_add.setOnClickListener(this);
        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);

        updateText();
    }

    public void setData(Anchor anchorBean) {
        this.anchorBean = anchorBean;
        if (anchorBean != null) {
            mPrice = anchorBean.getShares_per_one();
            tv_bought_count.setText("(已认购" + anchorBean.getBought_shares() + "次)");
        }
    }

    public void setAppointDialogListener(AppointDialogListener appointDialogListener) {
        this.appointDialogListener = appointDialogListener;
    }

    @Override
    public void onClick(View view) {
        if (view == btn_cancel) {
            if (appointDialogListener != null) {
                appointDialogListener.onCancel();
                dismiss();
            }
        } else if (view == btn_ok) {
            if (appointDialogListener != null && anchorBean != null) {
                appointDialogListener.onBuy(anchorBean.getVerify_id(), count * mPrice);
                dismiss();
            }
        } else if (view == btn_minus) {
            if (count > 1) {
                count--;
                updateText();
            }
        } else if (view == btn_add) {
            count++;
            updateText();
        }
    }

    private void updateText() {
        tv_count.setText("" + count);
        tv_coins.setText(count * mPrice + "钻石");
    }
}
